package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.employee;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.iot.jobstandardmanagement.common.enums.BootstrapUtil;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EmployeeCertificateMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.FileMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.EmployeeCertificate;
import com.cqhilink.iot.jobstandardmanagement.facade.model.EmployeeCertificateVo;
import com.cqhilink.iot.jobstandardmanagement.facade.model.File;
import com.cqhilink.iot.jobstandardmanagement.facade.service.employee.JsmEmployeeCertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by zmling on 2016/11/24.
 * 人员证书信息
 */

public class JsmEmployeeCertificateServiceImpl implements JsmEmployeeCertificateService {

	@Autowired
	private EmployeeCertificateMapper employeeCertificateMapper;
	@Autowired
	private FileMapper fileMapper;

	/**
	 * 新增
	 *
	 * @param employeeCertificate
	 * @throws Exception
	 */
	@Override
	public void addEmployeeCertificate(EmployeeCertificate employeeCertificate) throws Exception {
		employeeCertificateMapper.insertSelective(employeeCertificate);
	}

	/**
	 * 列表查询
	 *
	 * @param employeeCertificateVo
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONObject findEmployeeCertificateList(EmployeeCertificateVo employeeCertificateVo) throws Exception {
		JSONObject jsonObject = new JSONObject();
		employeeCertificateVo.setLimit(employeeCertificateVo.getLimit() == null ? 10 : employeeCertificateVo.getLimit());
		employeeCertificateVo.setOffset(employeeCertificateVo.getOffset() == null ? 0 : employeeCertificateVo.getOffset());
		employeeCertificateVo.setFilterMap(BootstrapUtil.filterStringToMap(employeeCertificateVo.getFilter()));
		if (employeeCertificateVo.getSearch() != null && employeeCertificateVo.getSearch() != "") {
			employeeCertificateVo.setSearch(employeeCertificateVo.getSearch().trim());
		}
		jsonObject.put("total", employeeCertificateMapper.queryCount(employeeCertificateVo));
		List<EmployeeCertificateVo> ecvList = employeeCertificateMapper.queryList(employeeCertificateVo);
//		if (ecvList != null && ecvList.size() > 0) {
//			List<File> fList = fileMapper.queryByBusinessIdObject(ecvList);
//			for (EmployeeCertificateVo param : ecvList) {
//				equalObject(param, fList);
//			}
//		}
		jsonObject.put("rows", JSON.toJSON(ecvList));
		return jsonObject;
	}

	/**
	 * 验证对应证书
	 *
	 * @param param
	 * @param list
	 */
	private void equalObject(EmployeeCertificateVo param, List<File> list) {
		if (list != null && list.size() > 0) {
			List<File> uploadedFiles = new ArrayList<File>();
			Iterator<File> it = list.iterator();
			while (it.hasNext()) {
				File file = it.next();
				if (param.getDocPath().contains(file.getFileName()) && param.getEmployeeCertificateId().equals(file.getBusinessId())) {
					uploadedFiles.add(file);
					it.remove();
				}
			}
			param.setUploadedFiles(uploadedFiles);
		}
	}

	/**
	 * 修改
	 *
	 * @param employeeCertificate
	 * @throws Exception
	 */
	@Override
	public void updateEmployeeCertificate(EmployeeCertificate employeeCertificate) throws Exception {
		employeeCertificate.setModifierId("11");
		employeeCertificate.setModifyTime(new Date());
		employeeCertificateMapper.updateByPrimaryKeySelective(employeeCertificate);
	}

	/**
	 * 根据主键查询证书
	 *
	 * @param employeeCertificateId
	 * @return
	 * @throws Exception
	 */
	@Override
	public EmployeeCertificateVo findEmployeeCertificate(String employeeCertificateId) throws Exception {
		return employeeCertificateMapper.selectByPrimaryKey(employeeCertificateId);
	}

	/**
	 * 删除证书
	 *
	 * @param certificate
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteCertificate(String certificate) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> list = new ArrayList<String>();
		list.add(certificate);
		map.put("certificate", list);
		//删除掉证书对应的附件信息
		fileMapper.delByBusinessId(certificate);
		return employeeCertificateMapper.updateCertificate(map);
	}
}
